/*
 * Ext JS Library 2.1
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 *
 * http://extjs.com/license
 */
var tabs;
Ext.apply(Ext.form.VTypes, {
    daterange: function(val, field){
        var date = field.parseDate(val);
        
        // We need to force the picker to update values to recaluate the disabled dates display
        var dispUpd = function(picker){
            var ad = picker.activeDate;
            picker.activeDate = null;
            picker.update(ad);
        };
        if (field.startDateField) {
            var sd = Ext.getCmp(field.startDateField);
            sd.maxValue = date;
            if (sd.menu && sd.menu.picker) {
                sd.menu.picker.maxDate = date;
                dispUpd(sd.menu.picker);
            }
        }
        else if (field.endDateField) {
            var ed = Ext.getCmp(field.endDateField);
            ed.minValue = date;
            if (ed.menu && ed.menu.picker) {
                ed.menu.picker.minDate = date;
                dispUpd(ed.menu.picker);
            }
        }
        /* Always return true since we're only using this vtype
         * to set the min/max allowed values (these are tested
         * for after the vtype test)
         */
        return true;
    },
    
    password: function(val, field){
        if (field.initialPassField) {
            var pwd = Ext.getCmp(field.initialPassField);
            return (val == pwd.getValue());
        }
        return true;
    },
    passwordText: '密码不匹配'
});
Ext.onReady(function(){
    Ext.QuickTips.init();
    tabs = new Ext.TabPanel({
        id: 'tabs',
        region: 'center',
        activeTab: 0,
        width: '100%',
        height: 700,
        plain: true,
        defaults: {
            autoScroll: true
        },
        items: [{
                id: 'tab_happy_right',
                title: '欢迎登录!',
                autoLoad: {
                    url: '/index/happy_welcome', //params: 'id=1'
                    scripts: true
                }
            }]
    });
    
    var flightActions = new Ext.Panel({
        frame: true,
        title: '航班查询',
        collapsible: true,
        contentEl: 'flights_mangaer',
        titleCollapse: true
    });
    
    var priceActions = new Ext.Panel({
        frame: true,
        title: '运价管理',
        collapsible: true,
        contentEl: 'price_manager',
        titleCollapse: true
    });
    
    var ordersActions = new Ext.Panel({
        frame: true,
        title: '定单管理(代理商专用)',
        collapsible: true,
        contentEl: 'orders_manager',
        titleCollapse: true
    });
	
    var bookingOrdersActions = new Ext.Panel({
        frame: true,
        title: '定单管理(供应商专用)',
        collapsible: true,
        contentEl: 'booking_orders_manager',
        titleCollapse: true
    });
    
    var paymentPlatformActions = new Ext.Panel({
        frame: true,
        title: '支付平台管理',
        collapsible: true,
        contentEl: 'payment_platform_manager',
        titleCollapse: true
    });
    
    var userActions = new Ext.Panel({
        frame: true,
        title: '我的资料',
        collapsible: true,
        contentEl: 'my_infos',
        titleCollapse: true
    });
    var actionPanel = new Ext.Panel({
        id: 'action-panel',
        region: 'west',
        split: true,
        collapsible: true,
        collapseMode: 'mini',
        width: 160,
        minWidth: 150,
        border: false,
        baseCls: 'x-plain',
        items: [flightActions,priceActions, ordersActions,bookingOrdersActions, paymentPlatformActions, userActions]
    });
    new Ext.Viewport({
        enableTabScroll: true,
        layout: "border",
        items: [{
                title: "面板",
                region: "north",
                height: 50,
                html: "<h1>"+USER_NAME+",您好,欢迎登录</h1>"
            }, actionPanel, tabs]
    });
    //tabs.getActiveTab().getUpdater().update("/index/index");
    //Ext.MessageBox.alert('提示', tabs.getActiveTab().getUpdater());
    //happy_right.getUpdater().update("http://www.163.com");
    
    var ab = actionPanel.body;
    ab.on('mousedown', doAction, null, {
        delegate: 'a'
    });
    ab.on('click', Ext.emptyFn, null, {
        delegate: 'a',
        preventDefault: true
    });
    
    var actions = {
        //普通运价
        'common_price_release': function(){
            var panel = tabs.getItem(0);
            panel.getUpdater().update('/ticket/common_price_release');
            panel.setTitle('普通运价发布');
            //Ext.MessageBox.alert('提示', '你好');
        },
        //普通运价列表
        'common_price_released_list': function(){
            var panel = tabs.getItem(0);
            panel.getUpdater().update('/ticket/common_price_released_list');
            //Ext.MessageBox.alert('提示', '你好');
        },
        //特殊仓位
        'special_position_release': function(){
            var panel = tabs.getItem(0);
            panel.getUpdater().update('/ticket/special_position_release');
        },
        //特殊仓位列表
        'special_position_release_list': function(){
            var panel = tabs.getItem(0);
            panel.getUpdater().update('/ticket/special_position_released_list');
        },

        'order_by_pnr': function(){
            var panel = tabs.getItem(0);
            panel.getUpdater().update('/ticket/rtPnr');
            panel.setTitle('PNR形成订单');
        },

        'special_offer_release': function(){
            var panel = tabs.getItem(0);
            panel.getUpdater().update('/ticket/special_offer_release');
            panel.setTitle('特殊价格发布');
            //Ext.MessageBox.alert('提示', '你好');
        },
        
        //定单管理之定单查询
        'query_agent_order': function(){
            tabs.getItem(0).getUpdater().update('/ticket/query_agent_order');
        },
        
        //退票管理
        'returned_order': function(){
            var panel = tabs.getItem(0);
            panel.getUpdater().update('/ticket/returned_order');
            panel.setTitle('<span class="red">退票管理</span>');
        },
        
        //供应商待订单列表
        'offer_booking_orders': function(){
            var panel = tabs.getItem(0);
            panel.getUpdater().update('/ticket/offer_booking_orders');
            panel.setTitle('订单处理');
        },
		
        //我的资料之基本信息
        'base_info': function(){
            var panel = tabs.getItem(0);
            panel.getUpdater().update('/web_user/base_info');
        },
        
        //我的资料之修改密码
        'password_info': function(){
            var panel = tabs.getItem(0);
            panel.getUpdater().update('/web_user/password_info');
        },
        
        //我的资料之退出系统
        'agent_logout': function(){
            window.document.location.href = "/index/agent_logout";
        },
        
        //航班查询
        'price_query': function(){
            var panel = tabs.getItem(0);
            panel.getUpdater().update('/ticket/price_query');
            panel.setTitle('航班查询');
        },
        
        //易宝支付平台管理
        'yeepay_manager': function(){
            var panel = tabs.getItem(0);
            panel.getUpdater().update('/ticket/yeepay_manager');
            panel.setTitle('易宝支付平台管理');
        },
        'action-delete': function(){
            Ext.Msg.confirm('Confirm', 'Are you sure you want to delete the selected task(s)?', function(btn){
                if (btn == 'yes') {
                    selections.each(function(s){
                        taskStore.remove(s);
                    });
                }
            });
        },
        
        'group-date': function(){
            taskStore.groupBy('dueDate');
        },
        
        'group-cat': function(){
            taskStore.groupBy('category');
        },
        
        'no-group': function(){
            taskStore.clearGrouping();
        }
    };
    
    function doAction(e, t){
        e.stopEvent();
        tabs.activate("tab_happy_right");
        //Ext.MessageBox.alert('提示', t.id);
        actions[t.id]();
		
		if (tabs.getItem(1)) {
			var panel = tabs.getItem(1);
			tabs.remove(panel);
		}
		if (tabs.getItem(2)) {
			var panel = tabs.getItem(2);
			tabs.remove(panel);
		}
		
    }
});
